package dao.impl;

import dao.RescheduleNotificationsDao;
import entity.RescheduleNotifications;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.DruidUtils;

import java.sql.Connection;
import java.util.List;

public class RescheduleNotificationsDaoImpl implements RescheduleNotificationsDao {
    @Override
    public List<RescheduleNotifications> queryAllById(Integer userId) {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "SELECT r.*  FROM reschedule_notifications r join appointments a \n" +
                    "    on appointment_id= a.id where user_id=? order by r.id desc";
            BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
            QueryRunner queryRunner = new QueryRunner();
            List<RescheduleNotifications> query = queryRunner.query(conn, s,
                    new BeanListHandler<>(RescheduleNotifications.class, basicRowProcessor), userId);
            DruidUtils.close(null, conn);
            return query;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return null;
    }

    @Override
    public int update(RescheduleNotifications rescheduleNotifications) {
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "delete from reschedule_notifications where id=?";
            QueryRunner queryRunner = new QueryRunner();
            int update = queryRunner.update(conn, s, id);
            DruidUtils.close(null, conn);
            return update;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return 0;
    }
}
